#include <cmath>
#include <iostream>
#include <queue>
using namespace std;

class Node {
   public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};

Node* connect(Node* root) {
    if (root == nullptr)
        return nullptr;
    queue<Node*> q;
    q.push(root);

    while (!q.empty()) {
        if(q.front() == nullptr) break;
        int size = q.size();
        for(int i=0;i<size;i++){
            if(i == size - 1){
                q.push(q.front()->left);
                q.push(q.front()->right);
                q.pop();
            }else{
                Node* tmp = q.front();
                q.pop();
                tmp->next = q.front();
                q.push(tmp->left);
                q.push(tmp->right);
            }
        }
    }
    return root;
}

int main() {
    cout << "hello world\n";
}